using System;
using UnityEngine.UIElements;
using MinesweeperPlus.Scripts.Configs;
using MinesweeperPlus.Scripts.Utils;

namespace MinesweeperPlus.Scripts.UI
{
    public sealed class ColorSchemePanel : UIElement
    {
        public new class UxmlFactory : UxmlFactory<ColorSchemePanel> { }

        #region Constants
        private const int NUMBER_OF_COLORS = 5;
        #endregion

        #region Actinos
        public Action<ColorSchemeConfig> OnSelect;
        #endregion

        #region Variables
        private Button _panelButton;
        private Label _nameLabel; 
        private IMGUIContainer _priceIcon; 
        private Label _priceLabel; 
        private VisualElement _colorsPanel; 
        private IMGUIContainer _selectIcon;

        private ColorSchemeConfig _colorSchemeConfig;
        private VisualElement[] _colorPanels;
        private bool _isBought;
        private bool _isSelected;
        #endregion
        #region Properties
        public bool IsBought
        {
            get => _isBought;
            set
            {
                if (_isBought == value)
                    return;

                _isBought = value;

                //_colorSchemeConfig.IsBought = _isBought;

                _priceIcon.SetDisplay(!_isBought);
                _priceLabel.SetDisplay(!_isBought);
            }
        }
        public bool IsSelected
        {
            get => _isSelected;
            set 
            {
                if (_isSelected == value)
                    return;

                _isSelected = value;
                _selectIcon.SetDisplay(_isSelected);
            }
        }
        #endregion

        #region ColorSchemePanel Methods
        public ColorSchemePanel() : base() { }
        public ColorSchemePanel(
            IconsConfig iconsConfig, 
            StyleSheet styleSheet,
            ColorSchemeConfig colorSchemeConfig) : base(iconsConfig, styleSheet) 
        {
            _colorSchemeConfig = colorSchemeConfig;

            SetInfo();
            ColorizePanel();
        }

        public ColorSchemeConfig GetColorSchemeConfig() => _colorSchemeConfig;
        #endregion

        #region Other Methods
        protected override void CreatePanel()
        {
            Panel = this;
            Panel.AddToClassList(Constants.UI_COLOR_SCHEME_PANEL_STYLE);

            _panelButton = CreateButton(
                () => OnSelect.Invoke(_colorSchemeConfig), 
                Constants.UI_COLOR_SCHEME_BUTTON_STYLE);

            _selectIcon = CreateIMGUIContainer(
                IconsConfig.ConfirmIcon, 
                Constants.UI_COLOR_SCHEME_SELECT_ICON_STYLE);
            _selectIcon.SetDisplay(false);

            _nameLabel = CreateLabel("Name");
            
            _priceIcon = CreateIMGUIContainer(IconsConfig.MineIcon);
            _priceIcon.AddToClassList(Constants.UI_COLOR_SCHEME_ICON_STYLE);

            _priceLabel = CreateLabel("999");

            var topPanel = CreatePanel(
                _selectIcon,
                _nameLabel,
                _priceIcon,
                _priceLabel);

            _colorsPanel = new VisualElement();
            _colorsPanel.AddToClassList(Constants.UI_COLOR_SCHEME_COLORS_PANEL_STYLE);

            _colorPanels = new VisualElement[NUMBER_OF_COLORS];
            for (int i = 0; i < NUMBER_OF_COLORS; i++)
            {
                var colorPanel = new VisualElement();
                colorPanel.AddToClassList(Constants.UI_COLOR_SCHEME_COLOR_PANEL_STYLE);
                _colorsPanel.Add(colorPanel);

                _colorPanels[i] = colorPanel;
            }
            _colorPanels[0]
                .AddToClassList(Constants.UI_COLOR_SCHEME_COLOR_PANEL_LEFT_STYLE);
            _colorPanels[NUMBER_OF_COLORS - 1]
                .AddToClassList(Constants.UI_COLOR_SCHEME_COLOR_PANEL_RIGHT_STYLE);

            var bottomPanel = CreatePanel(_colorsPanel);
        }

        private VisualElement CreatePanel(params VisualElement[] visualElements)
        {
            var panel = new VisualElement();
            panel.AddToClassList(Constants.UI_COLOR_SCHEME_SIDE_PANEL_STYLE);
            _panelButton.Add(panel);
            foreach (var visualElement in visualElements)
                panel.Add(visualElement);
            return panel;
        }
        private Label CreateLabel(string text, params string[] classNames)
        {
            var label = new Label(text);
            label.AddToClassList(Constants.UI_COLOR_SCHEME_LABEL_STYLE);
            foreach (var className in classNames)
                label.AddToClassList(className);
            return label;
        }

        private void SetInfo()
        {
            _nameLabel.text = _colorSchemeConfig.Name;
            _priceLabel.text = _colorSchemeConfig.Price.ToString();

            IsBought = _colorSchemeConfig.IsBought;
        }

        private void ColorizePanel()
        {
            Panel.SetBackgroundColor(_colorSchemeConfig.BackgroundColor);
            Panel.SetBorderColor(_colorSchemeConfig.BorderColor);

            _selectIcon.SetBackgroundImageColor(_colorSchemeConfig.IconColor);

            _nameLabel.SetTextColor(_colorSchemeConfig.TextColor);

            _priceIcon.SetBackgroundImageColor(_colorSchemeConfig.IconColor);
            _priceLabel.SetTextColor(_colorSchemeConfig.TextColor);

            _colorsPanel.SetBackgroundColor(_colorSchemeConfig.BorderColor);
            _colorsPanel.SetBorderColor(_colorSchemeConfig.BorderColor);

            _colorPanels[0].SetBackgroundColor(_colorSchemeConfig.CameraBackgroundColor);
            _colorPanels[1].SetBackgroundColor(_colorSchemeConfig.TextColor);
            _colorPanels[2].SetBackgroundColor(_colorSchemeConfig.IconColor);
            _colorPanels[3].SetBackgroundColor(_colorSchemeConfig.BackgroundColor);
            _colorPanels[4].SetBackgroundColor(_colorSchemeConfig.BorderColor);
        }
        #endregion
    }
}
